package com.atmosphere.util;

/**
 * <code>TimeConverter</code> defines the implementation of a
 * utility unit that provides the functionality of conversion
 * between animation time and standard 24-hour clock time.
 *
 * @author Yi Wang (Neakor)
 * @version Creation date: 05-19-2009 22:16 EST
 * @version Modified date: 05-19-2009 22:32 EST
 */
public class TimeConverter {

	/**
	 * Convert the given animation time to clock time.
	 * @param animTime The <code>Float</code> animation time.
	 * @param convertMin The <code>Boolean</code> flag of converting minute to standard 60-minute system.
	 * @param animLength The <code>Float</code> total animation length.
	 * @return The converted <code>Float</code> clock time <HH.mm>.
	 */
	public float animToClock(float animTime, boolean convertMin, float animLength) {
		// Get the raw time.
		final float raw = animTime/(animLength/24);
		// Convert minute.
		if(convertMin) {
			final int hr = (int)raw;
			final float rawMin = raw - hr;
			float min = (rawMin*60)/100;
			if(min >= 0.6f) min = 1;
			float time = hr + min;
			if(time >= 24) time = 0;
			return time;
		} else return raw;
	}
	
	/**
	 * Convert the given clock time to animation time.
	 * @param hr The <code>Integer</code> hour in 24-hour system.
	 * @param minute The <code>Integer</code> minute in 60-minute system.
	 * @param animLength The <code>Float</code> total animation length.
	 * @return The converted <code>Float</code> animation time.
	 */
	public float clockToAnim(int hr, int minute, float animLength) {
		// Calculate the total minutes elapsed.
		float totalMinute = hr * 60 + minute;
		// Convert the total minute into animation time system.
		float animationTime = (totalMinute/(24*60))*animLength;
		// Return the animation time.
		return animationTime;
	}
}
